package com.order.api.service.impl;

import com.order.api.constant.PersonGenderEnum;
import com.order.api.dao.UsersDao;
import com.order.api.po.Users;
import com.order.api.po.UsersExample;
import com.order.api.service.UsersService;
import com.order.api.vo.request.UserFindRequest;
import com.order.api.vo.request.UsersAddRequest;
import com.order.api.vo.request.UsersUpdateRequest;
import com.order.api.vo.response.UsersResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public UsersResponse findUser(UserFindRequest request) {
        //判断请求参数是否为空
        if (null==request){
            return new UsersResponse();
        }
        //设置redis的key值，需保证redis中的key都类似
        String key="user_" +request.getId();
        ValueOperations<String, Users> operations=redisTemplate.opsForValue();
        //判断redis中是否存在key
        boolean hasKey=redisTemplate.hasKey(key);
        if (hasKey){
            Users users= operations.get(key);
            UsersResponse response = new UsersResponse();
            BeanUtils.copyProperties(users,response);
            if (users.getGender().equals(PersonGenderEnum.MAN.getKey())){
                response.setGender(PersonGenderEnum.MAN.getTitle());
            }else {
                response.setGender(PersonGenderEnum.FEMALE.getTitle());
            }
            return response;
        }else {
            Users users = usersDao.selectByPrimaryKey(request.getId());
            if (users==null){
                return null;
            }
            operations.set(key,users,5, TimeUnit.HOURS);
            return UsersResponse.builder()
                                .address(users.getAddress())
                                .age(users.getAge())
                                .name(users.getName())
                                .gender(users.getGender().equals(PersonGenderEnum.MAN.getKey()) ? PersonGenderEnum.MAN.getTitle() : PersonGenderEnum.FEMALE.getTitle())
                                .phone(users.getPhone())
                                .build();
        }
    }

    @Override
    public boolean addUser(UsersAddRequest request) {
        //判断参数
        if (null==request){
            return false;
        }
        Users users = new Users();
        BeanUtils.copyProperties(request,users);
        int count = usersDao.insert(users);
        ValueOperations<String,Users> operations=redisTemplate.opsForValue();
        String key="user_"+users.getId();
        operations.set(key,users,5,TimeUnit.HOURS);
        return count == 1;
    }

    @Override
    public boolean updateUser(UsersUpdateRequest request) {
        //判断参数
        if (null == request){
            return false;
        }
        //更新数据库
        Users users = new Users();
        ValueOperations<String, Users> operations = redisTemplate.opsForValue();
        BeanUtils.copyProperties(request,users);
        int count = usersDao.updateByPrimaryKey(users);
        if (count!=0){
            String key="user_"+request.getId();
            boolean hasKey = redisTemplate.hasKey(key);
            if (hasKey){
                //删除redis中的数据
                redisTemplate.delete(key);
            }
            Users users1 = usersDao.selectByPrimaryKey(request.getId());
            if (users1!=null){
                //更新redis缓存
                operations.set(key,users1,5,TimeUnit.HOURS);
            }
        }
        return count == 1;
    }

    @Override
    public boolean deleteUser(UserFindRequest request) {
        if (request == null){
            return false;
        }
        int i = usersDao.deleteByPrimaryKey(request.getId());
        if (i==1){
            String key="user_"+request.getId();
            boolean hasKey = redisTemplate.hasKey(key);
            if (hasKey) {
                redisTemplate.delete(key);
            }
        }
        return i == 1;
    }

    @Override
    public Map<Long, String> getIdAndName(List<Long> ids) {
        UsersExample example=new UsersExample();
        example.createCriteria().andIdIn(ids);
        List<Users> list = usersDao.selectByExample(example);
        HashMap<Long, String> map = new HashMap<>();
        for (Users users : list) {
            map.put(users.getId(),users.getName());
        }
        return map;
    }
}
